<!DOCTYPE HTML>
<!--
	Dimension by HTML5 UP
	html5up.net | @ajlkn
	Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
-->
<html>
 <head>
  <title>
   Dimension by HTML5 UP
  </title>
  <!-- <meta charset="utf-8" /> -->
  <!-- <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> -->
  <meta charset="utf-8"/>
  <meta content="width=device-width,initial-scale=1.0" name="viewport"/>
  <link href="../../assets/css/article.css" rel="stylesheet"/>
  <link href="https://cdn.bootcss.com/highlight.js/9.15.8/styles/github.min.css" rel="stylesheet"/>
  <noscript>
   <link href="../../assets/css/noscript.css" rel="stylesheet"/>
  </noscript>
 </head>
 <body>
  <div id="app">
  </div>
  <!-- built files will be auto injected -->
 </body>
 <body class="is-preload">
  <!-- Wrapper -->
  <div id="wrapper">
   <!-- Main -->
   <div id="main">
    <article id="article">
     <h1 id="nagios4-ubuntu1604">
      nagios4 服务端安装指南（Ubuntu16.04）
     </h1>
     <hr/>
     <h2 id="_1">
      一、依赖安装
     </h2>
     <div class="codehilite">
      <pre><span></span><code><span class="err">sudo apt-get install wget build-essential apache2 php apache2-mod-php7.0 php-gd libgd-dev sendmail unzip</span>
</code></pre>
     </div>
     <h2 id="nagios-4">
      二、Nagios 4 安装
     </h2>
     <div class="codehilite">
      <pre><span></span><code><span class="n">sudo</span> <span class="n">useradd</span> <span class="n">nagios</span>
<span class="n">sudo</span> <span class="n">groupadd</span> <span class="n">nagcmd</span>
<span class="n">sudo</span> <span class="n">usermod</span> <span class="o">-</span><span class="n">a</span> <span class="o">-</span><span class="k">G</span> <span class="n">nagcmd</span> <span class="n">nagios</span>
<span class="n">apt</span><span class="o">-</span><span class="k">get</span> <span class="k">update</span>
<span class="n">apt</span><span class="o">-</span><span class="k">get</span> <span class="n">install</span> <span class="n">build</span><span class="o">-</span><span class="n">essential</span> <span class="n">libgd2</span><span class="o">-</span><span class="n">xpm</span><span class="o">-</span><span class="n">dev</span> <span class="n">openssl</span> <span class="n">libssl</span><span class="o">-</span><span class="n">dev</span> <span class="n">unzip</span>

<span class="n">cd</span> <span class="o">~</span>
<span class="n">curl</span> <span class="o">-</span><span class="n">L</span> <span class="o">-</span><span class="n">O</span> <span class="n">https</span><span class="p">:</span><span class="o">//</span><span class="n">assets</span><span class="p">.</span><span class="n">nagios</span><span class="p">.</span><span class="n">com</span><span class="o">/</span><span class="n">downloads</span><span class="o">/</span><span class="n">nagioscore</span><span class="o">/</span><span class="n">releases</span><span class="o">/</span><span class="n">nagios</span><span class="o">-</span><span class="mi">4</span><span class="p">.</span><span class="mi">3</span><span class="p">.</span><span class="mi">4</span><span class="p">.</span><span class="n">tar</span><span class="p">.</span><span class="n">gz</span>
<span class="n">tar</span> <span class="n">zxf</span> <span class="n">nagios</span><span class="o">-*</span><span class="p">.</span><span class="n">tar</span><span class="p">.</span><span class="n">gz</span>
<span class="n">cd</span> <span class="n">nagios</span><span class="o">-*</span>
<span class="p">.</span><span class="o">/</span><span class="n">configure</span> <span class="c1">--with-nagios-group=nagios --with-command-group=nagcmd</span>
<span class="n">make</span> <span class="k">all</span>
<span class="n">sudo</span> <span class="n">make</span> <span class="n">install</span>
<span class="n">sudo</span> <span class="n">make</span> <span class="n">install</span><span class="o">-</span><span class="n">commandmode</span>
<span class="n">sudo</span> <span class="n">make</span> <span class="n">install</span><span class="o">-</span><span class="n">init</span>
<span class="n">sudo</span> <span class="n">make</span> <span class="n">install</span><span class="o">-</span><span class="n">config</span>
<span class="n">sudo</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">bin</span><span class="o">/</span><span class="n">install</span> <span class="o">-</span><span class="k">c</span> <span class="o">-</span><span class="n">m</span> <span class="mi">644</span> <span class="n">sample</span><span class="o">-</span><span class="n">config</span><span class="o">/</span><span class="n">httpd</span><span class="p">.</span><span class="n">conf</span> <span class="o">/</span><span class="n">etc</span><span class="o">/</span><span class="n">apache2</span><span class="o">/</span><span class="n">sites</span><span class="o">-</span><span class="n">available</span><span class="o">/</span><span class="n">nagios</span><span class="p">.</span><span class="n">conf</span>
<span class="n">sudo</span> <span class="n">usermod</span> <span class="o">-</span><span class="k">G</span> <span class="n">nagcmd</span> <span class="n">www</span><span class="o">-</span><span class="k">data</span>
</code></pre>
     </div>
     <h2 id="check_nrpe-plugin">
      三、check_nrpe Plugin 安装（用户检测远程客户端）
     </h2>
     <div class="codehilite">
      <pre><span></span><code><span class="err">cd ~</span>
<span class="err">curl -L -O https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-3.2.1/nrpe-3.2.1.tar.gz</span>
<span class="err">tar zxf nrpe-*.tar.gz</span>
<span class="err">cd nrpe-*</span>
<span class="err">./configure</span>
<span class="err">make check_nrpe</span>
<span class="err">sudo make install-plugin</span>
</code></pre>
     </div>
     <h2 id="_2">
      四、插件安装
     </h2>
     <div class="codehilite">
      <pre><span></span><code><span class="err">cd ~</span>
<span class="err">curl -L -O http://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz</span>
<span class="err">tar zxf nagios-plugins-*.tar.gz</span>
<span class="err">cd nagios-plugins-*</span>
<span class="err">./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl</span>
<span class="err">make</span>
<span class="err">sudo make install</span>
</code></pre>
     </div>
     <h2 id="nagios">
      五、Nagios配置文件设置
     </h2>
     <p>
      编辑文件 /usr/local/nagios/etc/nagios.cfg，将下面的语句前面的#号去掉
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="err">...</span>
<span class="err">#cfg_dir=/usr/local/nagios/etc/servers</span>
<span class="err">...</span>
</code></pre>
     </div>
     <p>
      改为：
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="err">cfg_dir=/usr/local/nagios/etc/servers</span>
</code></pre>
     </div>
     <p>
      手动建立相应的目录
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="err">sudo mkdir /usr/local/nagios/etc/servers</span>
</code></pre>
     </div>
     <p>
      编辑文件 sudo nano /usr/local/nagios/etc/objects/commands.cfg，在最后面加入下面的内容：
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="err">...</span>
<span class="err">define command{</span>
<span class="err">        command_name check_nrpe</span>
<span class="err">        command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$</span>
<span class="err">}</span>
</code></pre>
     </div>
     <p>
      接下来运行下面的命令，重启相应的服务
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="err">sudo a2enmod rewrite</span>
<span class="err">sudo a2enmod cgi</span>
</code></pre>
     </div>
     <p>
      运行下面的第一天命令后输入相应的密码，命令最后面为账号名称，后面用于网页登录
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="err">sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin</span>
<span class="err">sudo ln -s /etc/apache2/sites-available/nagios.conf /etc/apache2/sites-enabled/</span>
</code></pre>
     </div>
     <p>
      新建文件 /etc/systemd/system/nagios.service，写入下面的内容
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="k">[Unit]</span>
<span class="na">Description</span><span class="o">=</span><span class="s">Nagios</span>
<span class="na">BindTo</span><span class="o">=</span><span class="s">network.target</span>

<span class="k">[Install]</span>
<span class="na">WantedBy</span><span class="o">=</span><span class="s">multi-user.target</span>

<span class="k">[Service]</span>
<span class="na">Type</span><span class="o">=</span><span class="s">simple</span>
<span class="na">User</span><span class="o">=</span><span class="s">nagios</span>
<span class="na">Group</span><span class="o">=</span><span class="s">nagios</span>
<span class="na">ExecStart</span><span class="o">=</span><span class="s">/usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg</span>
</code></pre>
     </div>
     <p>
      首先启动Apache2后在启动nagios
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="err">sudo systemctl restart apache2</span>
<span class="err">sudo systemctl enable /etc/systemd/system/nagios.service</span>
<span class="err">sudo systemctl start nagios</span>
</code></pre>
     </div>
     <p>
      输入下网址 http://nagios_server_public_ip/nagios 即可查看效果
     </p>
     <h2 id="dbidbd">
      六、DBI和DBD安装
     </h2>
     <p>
      在Ubuntu16.04中进行相应的搜索dbi和dbd后选择包含mysql字段的进行安装即可
     </p>
     <h2 id="ndo2db">
      七、ndo2db的安装
     </h2>
     <p>
      安装之前请确保MySQL的正确安装
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="n">cd</span> <span class="o">/</span><span class="n">tmp</span>
<span class="n">wget</span> <span class="o">-</span><span class="n">O</span> <span class="n">ndoutils</span><span class="p">.</span><span class="n">tar</span><span class="p">.</span><span class="n">gz</span> <span class="n">https</span><span class="p">:</span><span class="o">//</span><span class="n">github</span><span class="p">.</span><span class="n">com</span><span class="o">/</span><span class="n">NagiosEnterprises</span><span class="o">/</span><span class="n">ndoutils</span><span class="o">/</span><span class="n">releases</span><span class="o">/</span><span class="n">download</span><span class="o">/</span><span class="n">ndoutils</span><span class="o">-</span><span class="mi">2</span><span class="p">.</span><span class="mi">1</span><span class="p">.</span><span class="mi">3</span><span class="o">/</span><span class="n">ndoutils</span><span class="o">-</span><span class="mi">2</span><span class="p">.</span><span class="mi">1</span><span class="p">.</span><span class="mi">3</span><span class="p">.</span><span class="n">tar</span><span class="p">.</span><span class="n">gz</span>
<span class="n">tar</span> <span class="n">xzf</span> <span class="n">ndoutils</span><span class="p">.</span><span class="n">tar</span><span class="p">.</span><span class="n">gz</span>
<span class="n">cd</span> <span class="o">/</span><span class="n">tmp</span><span class="o">/</span><span class="n">ndoutils</span><span class="o">-</span><span class="mi">2</span><span class="p">.</span><span class="mi">1</span><span class="p">.</span><span class="mi">3</span><span class="o">/</span>
<span class="p">.</span><span class="o">/</span><span class="n">configure</span>
<span class="n">make</span> <span class="k">all</span>
<span class="n">make</span> <span class="n">install</span>

<span class="n">cd</span> <span class="n">db</span><span class="o">/</span>
<span class="p">.</span><span class="o">/</span><span class="n">installdb</span> <span class="o">-</span><span class="n">u</span> <span class="s1">'ndoutils'</span> <span class="o">-</span><span class="n">p</span> <span class="s1">'ndoutils_password'</span> <span class="o">-</span><span class="n">h</span> <span class="s1">'localhost'</span> <span class="o">-</span><span class="n">d</span> <span class="n">nagios</span>
<span class="n">cd</span> <span class="p">..</span> 
</code></pre>
     </div>
     <p>
      修改文件/usr/local/nagios/etc/ndo2db.cfg的数据库配置
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="err">db_user=ndoutils</span>
<span class="err">db_pass=ndoutils_password</span>
</code></pre>
     </div>
     <div class="codehilite">
      <pre><span></span><code><span class="err">make install-config</span>
<span class="err">mv /usr/local/nagios/etc/ndo2db.cfg-sample /usr/local/nagios/etc/ndo2db.cfg</span>
<span class="err">sed -i 's/^db_user=.*/db_user=ndoutils/g' /usr/local/nagios/etc/ndo2db.cfg</span>
<span class="err">sed -i 's/^db_pass=.*/db_pass=ndoutils_password/g' /usr/local/nagios/etc/ndo2db.cfg</span>
<span class="err">mv /usr/local/nagios/etc/ndomod.cfg-sample /usr/local/nagios/etc/ndomod.cfg</span>
</code></pre>
     </div>
     <p>
      启动安装脚本不知道为什么不能运行了，只能在startup文件夹中运行default-init了
     </p>
     <p>
      接下来运行Apache2，在运行ndo2db，最后运行nagios
     </p>
     <p>
      使用下面的命令查看数据库有数据既为成功：
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="err">echo 'select * from nagios.nagios_logentries;' | mysql -u ndoutils -p'ndoutils_password'</span>
</code></pre>
     </div>
     <h2 id="_3">
      参考链接
     </h2>
     <ul>
      <li>
       <a href="https://www.howtoing.com/ubuntu-nagios/">
        如何在Ubuntu 16.04上安装Nagios服务器监控
       </a>
      </li>
      <li>
       <a href="https://sourceforge.net/projects/nagios/files/ndoutils-2.x/">
        ndoutils download
       </a>
      </li>
      <li>
       <a href="https://assets.nagios.com/downloads/nagioscore/docs/ndoutils/NDOUtils.pdf">
        NDOUTILS Documentation Version 2.x
       </a>
      </li>
      <li>
       <a href="https://www.cnblogs.com/jiang-dh/p/6409390.html">
        nagios 抓取数据插件ndoutils 学习
       </a>
      </li>
      <li>
       <a href="https://support.nagios.com/kb/article/ndoutils-installing-ndoutils-406.html">
        NDOUtils - Installing NDOUtils
       </a>
      </li>
     </ul>
    </article>
   </div>
   <!-- Footer -->
   <footer id="footer">
    <p class="copyright">
     © Untitled. Design:
     <a href="https://html5up.net">
      HTML5 UP
     </a>
     .
    </p>
   </footer>
  </div>
  <!-- BG -->
  <div id="bg">
  </div>
  <!-- Scripts -->
  <script src="../assets/js/jquery.min.js">
  </script>
  <script src="../assets/js/browser.min.js">
  </script>
  <script src="../assets/js/breakpoints.min.js">
  </script>
  <script src="../assets/js/util.js">
  </script>
  <script src="../assets/js/main.js">
  </script>
 </body>
</html>
